/**
 * jims
 */
package com.jims.master.modules.sys.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.sys.entity.BaseUnit;
import com.jims.master.modules.sys.entity.TUnitFz;
import com.jims.master.modules.sys.service.BaseUnitService;
import com.jims.master.modules.sys.service.TUnitFzService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Controller
 * @author 系统管理员
 * @version 2017-06-12
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/tUnitFz")
public class TUnitFzController extends BaseController {

	@Autowired
	private TUnitFzService tUnitFzService;
	@Autowired
	private BaseUnitService baseUnitService;
	
	@ModelAttribute
	public TUnitFz get(@RequestParam(required=false) String id) {
		TUnitFz entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tUnitFzService.get(id);
		}
		if (entity == null){
			entity = new TUnitFz();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(TUnitFz tUnitFz, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TUnitFz> page = tUnitFzService.findPage(new Page<TUnitFz>(request, response), tUnitFz); 
		model.addAttribute("page", page);
		model.addAttribute("tUnitFz", tUnitFz);
		return "modules/sys/tUnitFzIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(TUnitFz tUnitFz, Model model) {
		model.addAttribute("tUnitFz", tUnitFz);
		List<BaseUnit> list = baseUnitService.findList(new BaseUnit());
		model.addAttribute("list", list);
		return "modules/sys/tUnitFzForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(TUnitFz tUnitFz, Model model) {
	    StringData data=new StringData();
		int i=tUnitFzService.save(tUnitFz);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(TUnitFz tUnitFz) {
	    StringData data=new StringData();
		int i=tUnitFzService.delete(tUnitFz);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}

}